<template>
  <div class="login-page">
    <HeaderComponent />
    <div class="login-container">
      <div class="login-content">
        <h1 class="login-title">管理员登录</h1>
        <a-form
            :model="formState"
            name="normal_login"
            class="login-form"
            @finish="onFinish"
            @finishFailed="onFinishFailed"
        >
          <a-form-item
              name="account"
              :rules="[{ required: true, message: '请输入账号!' }]"
          >
            <a-input
                v-model:value="formState.account"
                size="large"
                placeholder="账号"
            >
              <template #prefix>
                <UserOutlined class="site-form-item-icon" />
              </template>
            </a-input>
          </a-form-item>

          <a-form-item
              name="password"
              :rules="[{ required: true, message: '请输入密码!' }]"
          >
            <a-input-password
                v-model:value="formState.password"
                size="large"
                placeholder="密码"
            >
              <template #prefix>
                <LockOutlined class="site-form-item-icon" />
              </template>
            </a-input-password>
          </a-form-item>

          <a-form-item>
            <a-button
                :disabled="disabled"
                type="primary"
                html-type="submit"
                class="login-form-button"
                size="large"
                :loading="loading"
            >
              登录
            </a-button>
          </a-form-item>
        </a-form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, computed } from 'vue';
import { UserOutlined, LockOutlined } from '@ant-design/icons-vue';
import { message } from 'ant-design-vue';
import HeaderComponent from "@/components/user/HeaderComponent.vue";
import { useAdminStore } from "@/stores/index.js";
import { useRouter } from "vue-router";

const adminStore = useAdminStore();
const { login, error, loading } = adminStore;
const formState = reactive({
  account: '',
  password: '',
});
const router = useRouter();

const onFinish = async () => {
  try {
    const success = await login(formState.account, formState.password);
    if (success) {
      message.success("登录成功");
      await router.push({ path: '/admin' });
    } else {
      message.error("账号不存在或密码错误");
    }
  } catch (err) {
    message.error(error || "登录失败，请重试");
  }
};

const onFinishFailed = errorInfo => {
  console.log('Failed:', errorInfo);
  message.error("表单验证失败，请检查输入");
};

const disabled = computed(() => {
  return !(formState.account && formState.password);
});
</script>

<style scoped>
.login-page {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background: linear-gradient(135deg, #FF6B6B, #4ECDC4);
}

.login-container {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
}

.login-content {
  width: 100%;
  max-width: 400px;
  background-color: rgba(255, 255, 255, 0.9);
  padding: 40px;
  border-radius: 10px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  animation: fadeIn 0.5s ease-out;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(-20px); }
  to { opacity: 1; transform: translateY(0); }
}

.login-title {
  margin-bottom: 30px;
  font-size: 24px;
  font-weight: bold;
  color: #333;
  text-align: center;
}

.login-form :deep(.ant-form-item) {
  margin-bottom: 24px;
}

.login-form :deep(.ant-input-affix-wrapper) {
  border-radius: 6px;
}

.login-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.login-form-forgot {
  font-size: 14px;
  color: #1890ff;
}

.login-form-button {
  width: 100%;
}

.register-link {
  text-align: center;
  margin-top: 16px;
  font-size: 14px;
}

.register-link a {
  color: #1890ff;
  font-weight: bold;
}

@media screen and (max-width: 480px) {
  .login-content {
    padding: 30px 20px;
  }

  .login-title {
    font-size: 20px;
  }
}
</style>